<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common/global.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>欢迎使用XXXX系统</title>
    <link rel="stylesheet" type="text/css"
          href="${staticPath}/resources/jslib/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css"
          href="${staticPath}/resources/jslib/bootstrap/css/bootstrap-table.css">
    <link rel="stylesheet" type="text/css"
          href="${staticPath}/resources/css/theme.css">
   <link rel="stylesheet"
          href="${staticPath}/resources/jslib/font-awesome/css/font-awesome.css">
    <style>
        .update {
            color: #333;
            margin-right: 5px;
        }
        .remove {
            color: red;
            margin-left: 5px;
        }
        .count {
            color: blue;
            margin-left: 5px;
        }
        .alert {
            padding: 0 14px;
            margin-bottom: 0;
            display: inline-block;
        }
    </style>
    <script src="${staticPath}/resources/jslib/jquery-1.8.0.js"
            type="text/javascript"></script>
    <script src="${staticPath}/resources/jslib/bootstrap/js/bootstrap.js"
            type="text/javascript"></script>
    <script src="${staticPath}/resources/jslib/bootstrap/js/bootstrap-table.js"
            type="text/javascript"></script>
    <script src="${staticPath}/resources/jslib/bootstrap/js/bootstrap-table-zh-CN.js"
            type="text/javascript"></script>
</head>
<body>

<div class="header">

    <h1 class="page-title">
        话题管理
    </h1>
</div>
<ul class="breadcrumb">
    <li>
        <a href="${path}/back/welcome">Home</a>
        <span class="divider">/</span>
    </li>
    <li class="active">
        话题列表
    </li>
</ul>




<div class="container">

    <table id="table"
           data-height="500"
           data-pagination="true"
           data-detail-formatter="detailFormatter"
           data-detail-view="true"
           data-show-columns="true"
           data-search="true"
           data-show-refresh="true"
           data-query-params="queryParams"
           data-toolbar=".toolbar">
        <thead>
        <tr>

            <th data-field="id" data-sortable="true">编号</th>
            <th data-field="pic"  data-sortable="true">配图</th>
            <th data-field="title"  data-sortable="true">话题标题</th>
            <th data-field="content"  data-sortable="true">发表的内容</th>
            <th data-field="time"  data-sortable="true">发表时间</th>
         <%--   <th data-field="replyCount"  data-sortable="true">回复数</th>--%>


            <th data-field="action"
                data-align="center"
                data-formatter="actionFormatter"
                data-events="actionEvents">Action</th>
        </tr>
        </thead>
    </table>
</div>

<div id="modal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title"></h4>
            </div>
            <div class="modal-body">


                <input type="hidden" class="form-control" name="id" placeholder="编号">

                <div class="form-group">
                    <label>话题标题</label>
                    <input type="text" class="form-control" name="title" placeholder="话题标题">
                    <span id="msg"></span>
                </div>

                <div class="form-group">
                    <label>发表的内容</label>
                    <input type="text" class="form-control" name="content" placeholder="发表的内容">

                </div>

                <div class="form-group">
                    <label>发表时间</label>
                    <input type="text" class="form-control" name="time" placeholder="发表时间">

                </div>

                <div class="form-group">
                    <label>回复数</label>
                    <input type="text" class="form-control" name="replyCount" placeholder="回复数">

                </div>



            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary submit">Submit</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>

    var API_URL ='http://' + "localhost" + ':8080/bbsindex/listbo';

    var $table = $('#table').bootstrapTable(
            {
                url: API_URL,
                onLoadSuccess:function(data){

                    for(var i=0;i < data.length;i++) {

                        for (var key in data[i]){
                            if (key=="pic"){
                                data[i].pic="<img src='${path}/upload/bbs/"+data[i].pic  +"' width='30px' height='30px'/>";
                            }
                        }
                    }
                    $table.bootstrapTable("load",data);
                }
            }),
            $modal = $('#modal').modal({show: false}),
            $alert = $('.alert').hide();

    $modal.on('show.bs.modal', function () {
        $("#msg").html("");
    })


    $(function () {
        // create event
        $('.create').click(function () {
            showModal($(this).text());
        });

        $modal.find('.submit').click(function () {
            var row = {};

            $modal.find('input[name]').each(function () {
                row[$(this).attr('name')] = $(this).val();
            });

            $.ajax({
                url: 'http://' + "localhost" + ':8080/section/update',
                type : 'post',
                contentType: 'application/json',
                data: JSON.stringify(row),
                success: function (msg) {

                    if(msg=="success"){

                        $modal.modal('hide');
                        $table.bootstrapTable('refresh');
                        showAlert( 'success');
                        $("#msg").html("");

                    }else{

                       $("#msg").html("用户名已存在!");
                    }

                },
                error: function () {
                    $modal.modal('hide');
                    showAlert(($modal.data('id') ? 'Update' : 'Create') + ' item error!', 'danger');
                }
            });
        });
    });

    function queryParams(params) {
        return {};
    }

    function actionFormatter(value) {
        return [

            '<a class="removebo" href="javascript:" title="移除从轮播图"><i class="glyphicon glyphicon-remove-circle"></i></a>',
            '<a class="updatepic" href="javascript:" title="更改配图"><i class="glyphicon glyphicon-edit"></i></a>',

        ].join('');
    }

    // update and delete events
    window.actionEvents = {
        'click .update': function (e, value, row) {
            showModal($(this).attr('title'), row);
        },
        'click .removebo': function (e, value, row) {
            if (confirm('确定要从轮波图中移除吗?')) {
                $.ajax({
                    url:  'http://' + "localhost" + ':8080/bbsindex/removebo',
                    type: 'get',
                    data: "id=" + row.id ,
                    success: function () {
                        $table.bootstrapTable('refresh');
                        showAlert('Delete item successful!', 'success');
                    },
                    error: function () {
                        showAlert('Delete item error!', 'danger');
                    }
                })
            }
        },
        'click .updatepic': function (e, value, row) {



            window.location.href='http://localhost:8080/bbsindex/toupdatebopic?id=' + row.id;



        }
    };

    function showModal(title, row) {
        row = row || {
                 username:'',
                 password:''
                }; // default row value

        $modal.data('id', row.id);
        $modal.find('.modal-title').text(title);
        for (var name in row) {
            $modal.find('input[name="' + name + '"]').val(row[name]);
        }
         $modal.find('select[name="' + "sectionType" + '"]').val(row['sectionType']);


        $modal.modal('show');
    }

    function showAlert(title, type) {
        $alert.attr('class', 'alert alert-' + type || 'success')
                .html('<i class="glyphicon glyphicon-check"></i> ' + title).show();
        setTimeout(function () {
            $alert.hide();
        }, 3000);
    }

    function detailFormatter(index, row) {
        var html = [];
        $.each(row, function (key, value) {
            if (key!=0&&key!="type"){

                html.push('<p><b>' + key + ':</b> ' + value + '</p>');
            }
        });
        return html.join('');
    }
</script>
</body>
</html>
